package ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.beans.PropertyVetoException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import database.relation.Table;

public class DrawArea extends JPanel{
    
    static int a = 0;
    
    private JScrollPane drawViewport;
    private JPanel drawClient;
    Dimension area = new Dimension(800, 800);
    private ArrayList<TableInnerFrame> listTables;
    
    public DrawArea() {
//        setLayout(null);
        super(new BorderLayout());
        
        listTables = new ArrayList<TableInnerFrame>();
        
        drawClient = new JPanel();
        drawClient.setLayout(null);
        drawClient.setBackground(Color.yellow);
        drawClient.setPreferredSize(area);
        
        drawViewport = new JScrollPane(drawClient);
        drawViewport.getVerticalScrollBar().setUnitIncrement(10);
        drawViewport.getHorizontalScrollBar().setUnitIncrement(10);
        
//        drawViewport.addMouseMotionListener(new MyMouseMotionListener());
        drawClient.addMouseListener(new MyMouseListener() {
        	public void mouseClicked(MouseEvent e) {
                System.out.println("thang nao click tau");
            }
        });
        drawClient.addMouseListener(new MyMouseListener() {
        	public void mouseClicked(MouseEvent e) {
                System.out.println("la may hay thang khac");
            }
        });
        drawClient.addMouseListener(new MyMouseListener() {
        	public void mouseClicked(MouseEvent e) {
                System.out.println("lai co thang khac click tau kia");
            }
        });
        
        add(drawViewport);
    }
    
    public JPanel getDrawClient() {
        return drawClient;
    }
    
    public void addTable(TableInnerFrame table) {
//        table.getComponent(1).addMouseMotionListener(new MyMouseMotionListener());
        this.listTables.add(table);
        this.drawClient.add(table);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Ong noi may");
        frame.setBounds(200, 200, 350, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        
        DrawArea test = new DrawArea();
        test.setBounds(20, 20, 300, 200);
        
        for (int i=0; i<3; i++) {
            Table tbl = new Table();
            
//            tbl.getListColumn().add("anh minh");
//            tbl.getListColumn().add("inuyasha");
//            tbl.getListColumn().add("michael bagwell");
            
            TableInnerFrame tblIFrame = new TableInnerFrame(tbl);
            tblIFrame.setTitle("INUYASHA DE " + i);
            tblIFrame.setBounds(20*i, 10*i, 150, 100);
            tblIFrame.setVisible(true);

            test.addTable(tblIFrame);
//            test.getDrawClient().add(tblIFrame);
        }

        frame.getContentPane().add(test);
        frame.setVisible(true);
    }
    
    public class MyMouseListener implements MouseListener {
        boolean shit = false;

        @Override
        public void mouseClicked(MouseEvent e) {
            System.out.println("click tau a?");
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {}

        @Override
        public void mousePressed(MouseEvent e) {}

        @Override
        public void mouseReleased(MouseEvent e) {
//            System.out.println("dung lai duoc roi");
//            shit = false;
        }
        
    }
    
    public class MyMouseMotionListener implements MouseMotionListener {

        @Override
        public void mouseDragged(MouseEvent e) {
            // TODO Auto-generated method stub
            int x = e.getX() + drawClient.getComponent(0).getX() - drawViewport.getHorizontalScrollBar().getValue();
            int y = e.getY();
            boolean somethingSelected = false;
            int i = 0;

            System.out.println("fuck drag " + x  + " - " + drawViewport.getWidth());
            
            int xOut = x - drawViewport.getWidth();
            
            int b = x - drawViewport.getWidth();
            if (b > 0 && x-drawViewport.getWidth()<drawViewport.getHorizontalScrollBar().getMaximum()) {
            	int X = drawClient.getComponent(0).getX();
            	int Y = drawClient.getComponent(0).getY();
//            	drawClient.getComponent(0).setLocation(X+xOut, Y);
            	System.out.println("what the fuck");
            	int a = drawViewport.getHorizontalScrollBar().getValue();
//            	drawViewport.getHorizontalScrollBar().setValue(x-drawViewport.getWidth());
            	System.out.println(x-drawViewport.getWidth()+"/"+drawViewport.getHorizontalScrollBar().getMaximum());
            }
            
            
            
            if (somethingSelected) {;
                int a = x + drawViewport.getHorizontalScrollBar().getValue();
                System.out.println("ong noi may ha? " + a + " " + area.width);
                
                if (x+drawViewport.getHorizontalScrollBar().getValue() > area.width) {
                    area.width++;
                }
                if (y > area.height) {
                    area.height++;
                }
            }
            drawClient.setPreferredSize(area);
            drawClient.revalidate();
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            // TODO Auto-generated method stub
            
        }
        
    }

}
